package com.demo.activityservicedemo.demo1

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.demo.activityservicedemo.R

/**
 * Created by zhangyuncai on 2024/6/26.
 * 了解activity与service的单向通信
 */
class Demo1Activity : AppCompatActivity() {

    lateinit var demo1Intent: Intent
    var asInterface: IDemo1AidlInterface? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_demo1)

        demo1Intent = Intent(this, Demo1Service::class.java)
        bindService(demo1Intent, connection, Context.BIND_AUTO_CREATE)
    }

    val connection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            asInterface = IDemo1AidlInterface.Stub.asInterface(service)
            val demo1Result = asInterface!!.demo1()
            Log.e("Demo1Activity","demo1Result:${demo1Result}")
        }

        override fun onServiceDisconnected(name: ComponentName?) {
        }

    }

    override fun onDestroy() {
        unbindService(connection)
        super.onDestroy()
    }


}